﻿/**
 * 
 */
package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;

import bean.Cart;
import bean.Order;
import bean.User;
import util.JdbcUtils;


public class orderDao {
	
	
	public static boolean addOrder(User user,Cart cart) {
		String sql = "INSERT INTO book_order VALUE(?,?,?,?,?,?,?,?,?,?)";
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = sdf.format(date);
		/*随机产生四位数*/
		Random ran = new Random();
		int nums = ran.nextInt(9000)%(9000-1000+1) + 1000;
		/*利用时间戳得到8位不重复的随机数*/
		long nowDate = date.getTime();
		/*将上面产生的随机数拼接成用户id*/
		String oid = Integer.toHexString((int)nowDate) + Integer.toString(nums);
		Connection conn = null;
		PreparedStatement pstmt = null;
		int row = 0;
		try {
			conn = JdbcUtils.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,oid);
			pstmt.setString(2,user.getId());
			pstmt.setString(3,cart.getBid());
			pstmt.setString(4,cart.getBname());
			pstmt.setString(5,user.getName());
			pstmt.setString(6,user.getPhone());
			pstmt.setString(7,user.getAddress());
			pstmt.setInt(8,cart.getAmount());
			pstmt.setBigDecimal(9,cart.getSum());
			pstmt.setString(10,dateStr);
			row = pstmt.executeUpdate();
			if(row > 0) {
				return true;
			}
			pstmt.close();
			conn.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	public static int queryCount(String id) {
		String sql = "SELECT COUNT(*) record FROM book_order WHERE id=?";
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtils.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, id);
			rs = pstmt.executeQuery();
			if(rs.next()) {
				return rs.getInt("record");
			}
			rs.close();
			pstmt.close();
			conn.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
		return 0;
	}
	public static Order[] queryorder(String id) {
		String sql = "SELECT * FROM book_order WHERE id=?";
		int record = 0;
		record = queryCount(id);
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;		
		Order[] order = new Order[record];
		for(int i = 0;i<record;i++) {
			order[i] = new Order();
		}
		try {
			conn = JdbcUtils.getConnection();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,id);
			rs = pstmt.executeQuery();
			int index = 0;
			while(rs.next()) {
				order[index].setOid(rs.getString("oid"));
				order[index].setBname(rs.getString("bname"));
				order[index].setName(rs.getString("name"));
				order[index].setPhone(rs.getString("phone"));
				order[index].setAddress(rs.getString("address"));
				order[index].setAmount(rs.getInt("amount"));
				order[index].setSum(rs.getBigDecimal("sum"));
				order[index].setCreate_time(rs.getTimestamp("create_time"));
				index++;
			}
			return order;
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			try {
				rs.close();
				pstmt.close();
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
}
